import numpy as np
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
from keras import regularizers
from keras.layers import Dropout
from keras.layers import BatchNormalization
from keras.optimizers import SGD
from keras.optimizers import Adam, RMSprop
from sklearn.metrics import classification_report
from sklearn.metrics import precision_score, accuracy_score, precision_recall_fscore_support

# from keras.layers.advanced_activations import LeakyReLU
# from tensorflow.keras.models import Sequential
# from tensorflow.keras.layers import Dense, Dropout, Flatten
# from tensorflow.keras.layers import Conv2D, MaxPooling2D
# from tensorflow.keras.losses import sparse_categorical_crossentropy
# from tensorflow.keras.optimizers import Adam
# from extra_keras_datasets import emnist
import tensorflow as tf
from tensorflow import keras

# load the dataset
train_ht = np.load("E:\\data_pi\\train_hts_d35.npy")
# train_text=train_text.reshape(len(train_text), 30*32)
train_dt = np.load("E:\\data_pi\\train_dts_d35_m.npy")
# train_aud = np.load('E:\\data_pi\\train_yamnet_embed_d32.npy')
# train_edit = np.load('E:\\data_pi\\train_edit_embed_d32.npy')
# k=np.load('E:\\models\\vae_fnd_0.05_0.3\\features\\vae_fnd_d32.npy')
# k=np.c_[test_text,test_im,test_aud,test_edit]
# split into input (X) and output (y) variables
# X = np.c_[train_text,train_im,train_aud,train_edit]
Xt = np.load("D:\\awstemp\\vae_impred_d35_train.npy")
X = np.c_[Xt, train_dt, train_ht]
# X=test_im
data = []
i = 2
y = np.load("E:\\data_pi\\train_label_d35.npy")[:, i]

# define the keras model
model = Sequential()
# model.add(Dense(128, input_dim=8675, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.2))

# model.add(Dense(1024, activation='relu',kernel_regularizer=regularizers.l2(0.6)))
# model.add(BatchNormalization())
# model.add(Dropout(0.2))
#

model.add(Dense(128, activation="relu", kernel_regularizer=regularizers.l2(0.3)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(Dense(64, activation="relu", kernel_regularizer=regularizers.l2(0.3)))
model.add(BatchNormalization())
model.add(Dropout(0.2))


# #
# model.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.2))
# model.add(Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.5))
# # #
# model.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.5))
# #
#
# model.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.5))
# #
# #
# # model = Sequential()
# model.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.5))
# #
# model.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.5))
# #
# #
# model.add(Dense(32, activation='relu',kernel_regularizer=regularizers.l2(0.3)))
# model.add(BatchNormalization())
# model.add(Dropout(0.5))


# model.add(Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.3)))

# model.add(BatchNormalization())
# model.add(Dense(32, activation='relu'))
# model.add(Dropout(0.2))
# model = Sequential()
# model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
# model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Dropout(0.25))
# model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
# model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Dropout(0.25))
# model.add(Flatten())
# model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation="sigmoid"))
# # compile the keras model

model.compile(loss="binary_crossentropy", optimizer=Adam(1e-5), metrics=["accuracy"])
# fit the keras model on the dataset
model.fit(X, y, epochs=300, validation_split=0.2, batch_size=32)
# evaluate the keras model
# test_text = np.load('E:\\data_pi\\test_text_embed_d32.npy')
# test_text=test_text.reshape(len(test_text), 30*32)
# test_im = np.load('E:\\data_pi\\test_image_embed_d32.npy')
# test_aud = np.load('E:\\data_pi\\test_yamnet_embed_d32.npy')
# test_edit = np.load('E:\\data_pi\\test_edit_embed_d32.npy')
# k=np.load('E:\\models\\vae_fnd_0.05_0.3\\features\\vae_fnd_d32_t.npy')
# k=np.c_[test_text,test_im,test_aud,test_edit]
test_ht = np.load("E:\\data_pi\\test_hts_d35.npy")
# train_text=train_text.reshape(len(train_text), 30*32)
test_dt = np.load("E:\\data_pi\\test_dts_d35_m.npy")


X_testt = np.load("D:\\awstemp\\vae_impred_d35_test.npy")
X_test = np.c_[X_testt, test_dt, test_ht]
# X=test_im
_, accuracy = model.evaluate(X_test, np.load("E:\\data_pi\\test_label_d35.npy")[:, i])
print("Accuracy: %.2f" % (accuracy * 100))
test_label = np.load("E:\\data_pi\\test_label_d35.npy")[:, i]
for i in range(10):
    pred = model.predict(X_test)
    pred[pred >= 0.5] = 1
    pred[pred < 0.5] = 0
    print(accuracy_score(test_label, pred))
    print(precision_recall_fscore_support(test_label, pred))

# print(classification_report(np.load('E:\\data_pi\\test_label_d32_sub.npy'),y_pred))
# print(y_pred)
